package com.hzqy.service.virtualchannel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.virtualchannel.VirtualChannelDao;
import com.hzqy.web.vo.virtualchannel.VirtualChannelMaterialVo;
import com.hzqy.web.vo.virtualchannel.VirtualChannelVo;

@Service
public class VirtualChannelServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private VirtualChannelDao virtualChannelDao;
	
	public List<VirtualChannelVo> selectChannelList(VirtualChannelVo virtualChannelVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		List<VirtualChannelVo> returnList  = new ArrayList<VirtualChannelVo>();
		map.put("f_vc_status", virtualChannelVo.getF_vc_status());//前端默认传进来5 ，投放状态
		map.put("f_vc_customid", virtualChannelVo.getF_vc_customid());
		map.put("f_vc_type", 1);
		List<VirtualChannelVo> results  = virtualChannelDao.selectChannelList( map);//频道列表
		int count =0;
		for(VirtualChannelVo channelVo : results){
			Map<String,Object> smmap = new HashMap<String, Object>();
			smmap.put("f_vcm_channelid", channelVo.getF_vc_id());
			List<VirtualChannelMaterialVo> malist = virtualChannelDao.selectMaterial(smmap);//频道素材列表
			
			virtualChannelVo = selectVirtualChannel(channelVo);//插播素材列表
			
			virtualChannelVo.setVirtualChannelMaterialVos(malist);
			
			returnList.add(count, virtualChannelVo);
			count++;
		}
		return returnList;
	}
	/**
	 * 查询一个频道 相同f_vc_parentid 下的插播频道，及其插播素材，插播可能有多个
	 * 在一个节目单下面可能有插播的节目，插播的节目现在的设计方式：把插播的节目当成一个节目单，节目单的子节目单
	 * @param virtualChannelVo
	 * @return
	 */
	public VirtualChannelVo selectVirtualChannel(VirtualChannelVo virtualChannelVo) {
		if(virtualChannelVo!=null){
			//查询插播列表
			Map<String,Object> childmap = new HashMap<String, Object>();
			childmap.put("f_vc_parentid", virtualChannelVo.getF_vc_id());
			childmap.put("f_vc_type", 2);//f_vc_type 类型，1，正常编辑节目 2，插播节目
			childmap.put("f_vc_customid", virtualChannelVo.getF_vc_customid());
			childmap.put("f_vc_status", virtualChannelVo.getF_vc_status());
			
			//查询节目单下的子节目单(查询插播的节目，插播的节目可能会有多次插播)
			List<VirtualChannelVo> childVc = virtualChannelDao.selectChannelList(childmap);
			
			for(VirtualChannelVo vv : childVc){
				Map<String,Object> smmap = new HashMap<String, Object>();
				smmap.put("f_vcm_channelid", vv.getF_vc_id());
				vv.setVirtualChannelMaterialVos(virtualChannelDao.selectMaterial(smmap));
			}
			virtualChannelVo.setInterCutvirtualChannelVos(childVc);
		}
		return virtualChannelVo;
	}
	
}
